package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RequestMapping("/admin/common")
@RestController
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;


    @PostMapping("/upload")
    public Result upload(MultipartFile file){
        //获取文件名字
        String originalFilename = file.getOriginalFilename();
        //将名字最后的格式提取出来
        String substring = originalFilename.substring(originalFilename.indexOf("."));
        //将名字换成uuid的形式，避免名字重复替换掉
        String fileNmae = UUID.randomUUID().toString().replace("-","")+substring;

        try {
            //调用方法上传文件
            String upload = aliOssUtil.upload(file.getBytes(), fileNmae);
            return Result.success(upload);
        } catch (IOException e) {
            return Result.error(e.getMessage());
        }
    }
}
